package com.javacoo.lock.starter;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.javacoo.xkernel.spi.ExtensionLoader;
import com.javacoo.lock.client.api.Lock;
import com.javacoo.lock.client.aspect.LockAspect;
import com.javacoo.lock.client.config.LockConfig;

import lombok.extern.slf4j.Slf4j;

/**
 * 自动配置类
 * <li></li>
 * @author duanyong
 * @date 2021/3/5 9:50
 */
@Slf4j
@Configuration                                                                                                                              
@EnableConfigurationProperties(value = LockConfig.class)
@ConditionalOnClass(Lock.class)
@ConditionalOnProperty(prefix = LockConfig.PREFIX, value = LockConfig.ENABLED, matchIfMissing = true)
public class LockAutoConfiguration {
	@Autowired                                                                                                                              
    private LockConfig lockConfig;
	@Bean
	@ConditionalOnMissingBean(Lock.class)
	public Lock createLock() {
        log.info("初始化分布式锁,实现类名称:{}",lockConfig.getImpl());
        LockHolder.lock = ExtensionLoader.getExtensionLoader(Lock.class).getExtension(lockConfig.getImpl());
        log.info("初始化分布式锁成功,实现类:{}",LockHolder.lock);
        return LockHolder.lock;
	}
    @Bean
    public LockAspect createLockAspect() {
        return new LockAspect();
    }
}
